// pages/others/history/history.js
var util = require('../../../utils/util.js');
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    haveImg: app.globalData.haveImg,
    bgimg1: app.globalData.bgimg,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/getHistorySystemInform',
      method:'POST',
      data:{
        token:app.globalData.userInfo.token
      },
      success:function(e)
      {
        console.log(e)
        that.setData({
          bgimg1: app.globalData.bgimg,
          haveImg: app.globalData.haveImg
        })
        var list = e.data.list
        for(var i=0;i<e.data.list.length;i++)
        {
          list[i].noticeTime = list[i].noticeTime.substring(0, 10)
         //根据情况处理显示
         //好友申请审核 得到头像
        
          var temp=JSON.parse(list[i].content)
          // console.log(temp)
          list[i].state=temp.state
          if (list[i].name == "好友申请审核") {
          list[i].avatar=temp.inviter.avatar
          }
          else if (list[i].name == "加入房间邀请") 
          {
            list[i].avatar = temp.invitee.avatar
          }
          else if (list[i].name == "添加好友申请") 
          {
            list[i].avatar = app.globalData.userInfo.userInfo.avatar
          }
          else if (list[i].name == "加入房间审核")
          {
            if (list[i].state!= "已同意")
            {
            list[i].avatar = temp.inviter.avatar
            }
            else
            {
              list[i].avatar = app.globalData.userInfo.userInfo.avatar
            }
          }
          else if (list[i].name == "加入房间申请") {
            list[i].avatar = app.globalData.userInfo.userInfo.avatar
          }
          
          that.setData({
            list:list
          })
        }
        }
    })

  },
  agree:function(e)
  {
    console.log(e)
    var roomId=e.currentTarget.dataset.room
    var userId = e.currentTarget.dataset.id
    if (e.currentTarget.dataset.type =="好友申请审核")
    {
      var content = JSON.parse(e.currentTarget.dataset.content)
      console.log(content.friend)
      content.friend.state=true
         wx.request({
          url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/agreeFriendRequest',
          method: 'POST',
          data: {
            token: app.globalData.userInfo.token,
            friend: content.friend
          }, success: function (e) {
            console.log(e)
          }
      })
      // 刷新列表
    }
    else if (e.currentTarget.dataset.type == "加入房间审核")
    {
      var content = JSON.parse(e.currentTarget.dataset.content)
      console.log(content.member)
      content.member.state = true
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/agreeJoinRequest',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token,
          member:content.member
          // friend: content.friend
        }, success: function (e) {
          console.log(e)
        }
      })
    }
   this.renew()
    
  },
  omit:function(e)
  {
    //拒绝
    if (e.currentTarget.dataset.type == "好友申请审核") {
      var content = JSON.parse(e.currentTarget.dataset.content)

      console.log(content.friend)
      content.friend.state = false
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/agreeFriendRequest',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token,
          friend: content.friend
        }, success: function (e) {
          console.log(e)
        }
      })
      // 刷新列表
    }
    else if (e.currentTarget.dataset.type == "加入房间审核") {
      var content = JSON.parse(e.currentTarget.dataset.content)
      console.log(content.member)
      content.member.state = false
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/agreeJoinRequest',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token,
          member: content.member
          // friend: content.friend
        }, success: function (e) {
          console.log(e)
        }
      })
    }
    this.renew()

  },
  renew:function()
  {
    var that = this
    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/getHistorySystemInform',
      method: 'POST',
      data: {
        token: app.globalData.userInfo.token
      },
      success: function (e) {
        console.log(e)
        that.setData({
          // list:e.data.list,
          bgimg1: app.globalData.bgimg,
          haveImg: app.globalData.haveImg
        })
        var list = e.data.list
        for (var i = 0; i < e.data.list.length; i++) {
          list[i].noticeTime = list[i].noticeTime.substring(0, 10)
          //根据情况处理显示
          //好友申请审核 得到头像

          var temp = JSON.parse(list[i].content)
          // console.log(temp)
          list[i].state = temp.state
          if (list[i].name == "好友申请审核") {
            list[i].avatar = temp.inviter.avatar
          }
          else if (list[i].name == "加入房间邀请") {
            list[i].avatar = temp.invitee.avatar
          }
          else if (list[i].name == "添加好友申请") {
            list[i].avatar = app.globalData.userInfo.userInfo.avatar
          }
          else if (list[i].name == "加入房间审核") {
            if (list[i].state != "已同意") {
              list[i].avatar = temp.inviter.avatar
            }
            else {
              list[i].avatar = app.globalData.userInfo.userInfo.avatar
            }
          }
          else if (list[i].name == "加入房间申请") {
            list[i].avatar = app.globalData.userInfo.userInfo.avatar
          }

          that.setData({
            list: list
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //刷新
    this.renew()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (e) {
    //刷新
    console.log(e)
    var that = this
    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/getHistorySystemInform',
      method: 'POST',
      data: {
        token: app.globalData.userInfo.token
      },
      success: function (e) {
        console.log(e)
        that.setData({
          // list:e.data.list,
          bgimg1: app.globalData.bgimg,
          haveImg: app.globalData.haveImg
        })
        var list = e.data.list
        for (var i = 0; i < e.data.list.length; i++) {
          list[i].noticeTime = list[i].noticeTime.substring(0, 10)
          //根据情况处理显示
          //好友申请审核 得到头像

          var temp = JSON.parse(list[i].content)
          // console.log(temp)
          list[i].state = temp.state
          if (list[i].name == "好友申请审核") {
            list[i].avatar = temp.inviter.avatar
          }
          else if (list[i].name == "加入房间邀请") {
            list[i].avatar = temp.invitee.avatar
          }
          else if (list[i].name == "添加好友申请") {
            list[i].avatar = app.globalData.userInfo.userInfo.avatar
          }
          else if (list[i].name == "加入房间审核") {
            if (list[i].state != "已同意") {
              list[i].avatar = temp.inviter.avatar
            }
            else {
              list[i].avatar = app.globalData.userInfo.userInfo.avatar
            }
          }
          else if (list[i].name == "加入房间申请") {
            list[i].avatar = app.globalData.userInfo.userInfo.avatar
          }

          that.setData({
            list: list
          })
        }
      }
    })


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})